package com.xinjie.www.dao.user;

import com.xinjie.www.dao.BaseDao;
import com.xinjie.www.po.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserDaoImpl implements UserDao {
    //获取登录用户
    @Override
    public User getLoginUser(Connection connection, String userName, String password) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        User user = null;
        if (connection != null) {
            String sql = "select * from user where username=? and password=?";
            Object[] params = {userName, password};
            rs = BaseDao.excute(connection, pstm, rs, sql, params);

            if (rs.next()) {
                user = new User();
                user.setUserId(rs.getInt("user_id"));
                user.setUserName(rs.getString("userName"));
                user.setPassword(rs.getString("password"));
                user.setSex(rs.getString("sex"));
                user.setBirthday(rs.getString("birthday"));
                user.setSign(rs.getString("sign"));
            }
            boolean b = BaseDao.closeResource(null, pstm, rs);
            if (b) {
                System.out.println("关闭成功");
            }
        }
        return user;
    }

    //修改用户名
    @Override
    public int updateUsername(Connection connection, int id, String newUsername, String oldUsername) throws SQLException {
        PreparedStatement pstm = null;
        int excute1 = 0;
        if (connection != null) {
            String sql1 = "update user set userName = ? where user_id = ?";
            Object[] params1 = {newUsername, id};
            excute1 = BaseDao.excute(connection, pstm, sql1, params1);
            if (excute1 > 0) {
                String sql2 = "update post set writer = ? where writer = ?";
                Object[] params2 = {newUsername, oldUsername};
                int excute2 = BaseDao.excute(connection, pstm, sql2, params2);
            }
            BaseDao.closeResource(null, pstm, null);
        }
        return excute1;
    }

    //修改密码
    @Override
    public int updatePassword(Connection connection, int id, String password) throws SQLException {
        PreparedStatement pstm = null;
        int excute = 0;
        if (connection != null) {
            String sql = "update user set password = ? where user_id = ?";
            Object params[] = {password, id};
            excute = BaseDao.excute(connection, pstm, sql, params);
            BaseDao.closeResource(null, pstm, null);
        }
        return excute;
    }

    //更新个性签名
    @Override
    public int updateSign(Connection connection, int id, String sign) throws SQLException {
        PreparedStatement pstm = null;
        int excute = 0;
        if (connection != null) {
            String sql = "update user set sign = ? where user_id = ?";
            Object params[] = {sign, id};
            excute = BaseDao.excute(connection, pstm, sql, params);
            BaseDao.closeResource(null, pstm, null);
        }
        return excute;
    }

    //注册用户
    @Override
    public int registerUser(Connection connection, String name, String password, String sex, String birthday, String sign) throws SQLException {
        PreparedStatement pstm = null;
        int excute = 0;
        if (connection != null) {
            String sql = "insert into user (userName,password,sex,birthday,sign)values(?,?,?,?,?)";
            Object params[] = {name, password, sex, birthday, sign};
            excute = BaseDao.excute(connection, pstm, sql, params);
            BaseDao.closeResource(null, pstm, null);
        }
        return excute;
    }


    //获取用户列表
    @Override
    public List<User> getAllUser(Connection connection) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        Object[] params = {};
        List<User> list = new ArrayList<>();
        String sql = "select * from user";
        pstm = connection.prepareStatement(sql);
        rs = BaseDao.excute(connection, pstm, rs, sql, params);
        while (rs.next()) {
            User user = new User();
            user.setUserId(rs.getInt("user_id"));
            user.setUserName(rs.getString("userName"));
            user.setPassword(rs.getString("password"));
            user.setSex(rs.getString("sex"));
            user.setBirthday("birthday");
            user.setSign(rs.getString("sign"));
            list.add(user);
        }
        BaseDao.closeResource(null, pstm, rs);
        return list;
    }

    //获取某贴吧下被封禁的用户
    @Override
    public List<User> getBanUser(Connection connection, int tiebaId) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<User> users = new ArrayList<>();
        if (connection != null) {
            String sql = "select u.* from user u,ban b where u.user_id=b.user_id and b.tieba_id=? ";
            Object[] params = {tiebaId};
            pstm = connection.prepareStatement(sql);
            rs = BaseDao.excute(connection, pstm, rs, sql, params);
            while (rs.next()) {
                User user = new User();
                user.setUserId(rs.getInt("user_id"));
                user.setUserName(rs.getString("userName"));
                user.setPassword(rs.getString("password"));
                user.setSex(rs.getString("sex"));
                user.setBirthday("birthday");
                user.setSign(rs.getString("sign"));
                users.add(user);
            }
            BaseDao.closeResource(null, pstm, rs);
        }
        return users;
    }
}
